from langchain_deepseek import ChatDeepSeek
from pydantic import BaseModel, Field
from typing import Optional
# 读取环境变量，
from dotenv import load_dotenv
from langchain_openai import ChatOpenAI
from typing import List
load_dotenv()
# apikey="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"
# baseurl="https://api.openai.com/v1"

def news_parser(message:str):
    print(message)
    class news(BaseModel):
        title: Optional[str] = Field(None, description="The title of the news")
        date: Optional[str] = Field(None, description="The date of the news")
        content: Optional[str] = Field(None, description="The content of the news")
        source: Optional[str] = Field(None, description="The source of the news")
        link: Optional[str] = Field(None, description="The link of the news")

    class newsList(BaseModel):
        newsList: List[news] = Field(default=[], description="BIM、智能建造、智慧城市、智慧交通等跟建筑数字化相关的政策和新闻热点")
        class Config:
            json_schema_extra = {
                "description": "提取BIM、智能建造、智慧城市、智慧交通等跟建筑数字化相关的政策和新闻热点"
            }
    # model = ChatOpenAI(model="gpt-4o-mini", temperature=0)
    # Bind the schema to the model.
    model = ChatOpenAI(
    model="qwen-plus-2025-04-28",
    api_key="sk-c11908b122d94662b3e16f2d958af3f0",
    base_url="https://dashscope.aliyuncs.com/compatible-mode/v1",
    temperature=0,
)
    model_with_structure = model.with_structured_output(newsList)
    # Invoke the model
    structured_output = model_with_structure.invoke(message)
    print (structured_output)
    return structured_output

def create_wall_output_parser(message:str):
    class Point(BaseModel):
        x: float = Field(..., description="The x-coordinate of the point")
        y: float = Field(..., description="The y-coordinate of the point")
        z: float = Field(..., description="The z-coordinate of the point")

    class WallInfo(BaseModel):
        startPoint: Optional[Point] = Field(None, description="The start point of the wall")
        endPoint: Optional[Point] = Field(None, description="The end point of the wall")
        height: Optional[float] = Field(None, description="The height of the wall")
        thickness: Optional[float] = Field(None, description="The thickness of the wall")

        class Config:
            json_schema_extra = {
                "description": "Information of a wall in Revit"
            }
    model = ChatDeepSeek(
            model="deepseek-chat",
            temperature=0,
            max_tokens=None,
            timeout=None,
            max_retries=2,
            api_key="sk-c29b014fa48f4a95bca0b44455f55ea9",
            # other params...
        )
    # Bind the schema to the model
    model_with_structure = model.with_structured_output(WallInfo)
    # Invoke the model
    structured_output = model_with_structure.invoke(message)
    print (structured_output)
    return structured_output


